'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: document_handler.py
* @Time: 2025/9/26
* @All Rights Reserve By Brtc
'''
from dataclasses import dataclass
from uuid import UUID
from flask import request
from flask_login import login_required, current_user
from injector import inject
from internal.schema.dataset_schema import (GetDocumentsWithPageReq,
                                            GetDocumentsWithPageResp,
                                            GetDocumentResp,
                                            UpdateDocumentNameReq)

from internal.schema.document_schema import (CreateDocumentReq,
                                             CreateDocumentResp,
                                             UpdateDocumentReq)

from internal.service import DocumentService
from pkg.paginator.paginator import PageModel
from pkg.response import validate_error_json, success_json, success_message


@inject
@dataclass
class DocumentHandler:
    """文档处理器"""
    document_service: DocumentService

    @login_required
    def create_documents(self, dataset_id:UUID):
        """在指定知识库上面新增文档"""
        #1、提取请求并校验
        req = CreateDocumentReq()
        if not req.validate():
            return validate_error_json(req.errors)

        #2、调用服务并创建文档,返回文档列表信息 + 处理批次
        documents, batch = self.document_service.create_documents(dataset_id, current_user,**req.data)
        #3、生成响应结构并返回
        resp = CreateDocumentResp()
        return success_json(resp.dump((documents,batch)))


    @login_required
    def get_documents_status(self, dataset_id:UUID, batch:str):
        """根据传递的知识库id + 批处理的标识 获取文档的状态"""
        document_status = self.document_service.get_document_status(dataset_id, batch, current_user)
        return success_json(document_status)


    @login_required
    def update_document_enabled(self, dataset_id: UUID, document_id: UUID):
        """根据传递的知识库id + 文档id 更新指定文档的启用状态"""
        # 1、提取请求并校验
        req = UpdateDocumentReq()
        if not req.validate():
            return validate_error_json(req.errors)
        #2、调用服务更新指定文档状态
        self.document_service.update_document_enabled(dataset_id, document_id, req.enabled.data, current_user)
        return success_message("更新文档状态成功！！")


    @login_required
    def get_document(self, dataset_id: UUID, document_id: UUID):
        """根据传递的知识库id+文档id获取文档详情信息"""
        document = self.document_service.get_document(dataset_id, document_id, current_user)
        resp = GetDocumentResp()
        return success_json(resp.dump(document))


    @login_required
    def update_document_name(self, dataset_id: UUID, document_id: UUID):
        """根据传递的知识库id+文档id更新对应文档的名称信息"""
        # 1.提取请求并校验数据
        req = UpdateDocumentNameReq()
        if not req.validate():
            return validate_error_json(req.errors)
        # 2.调用服务更新文档的名称信息
        self.document_service.update_document(dataset_id, document_id, current_user,name=req.name.data)
        return success_message("更新文档名称成功")


    @login_required
    def get_documents_with_page(self, dataset_id: UUID):
        """根据传递的知识库id获取文档分页列表数据"""
        # 1.提取请求数据并校验
        req = GetDocumentsWithPageReq(request.args)
        if not req.validate():
            return validate_error_json(req.errors)
        # 2.调用服务获取分页列表数据以及分页数据
        documents, paginator = self.document_service.get_documents_with_page(dataset_id, req, current_user)
        # 3.构建响应结构并映射
        resp = GetDocumentsWithPageResp(many=True)
        return success_json(PageModel(list=resp.dump(documents), paginator=paginator))


    @login_required
    def delete_document(self, dataset_id: UUID, document_id: UUID):
        """根据传递的知识库id + 文档id 删除指定的文档信息"""
        self.document_service.delete_document(dataset_id, document_id, current_user)
        return success_message("删除文档成功！")


























